package com.stylefeng.guns.modular.flagglory.controller;

import com.stylefeng.guns.common.persistence.model.Division;
import com.stylefeng.guns.common.persistence.model.Level;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.flagglory.service.IDivisionService;
import com.stylefeng.guns.modular.flagglory.service.ILevelService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Robot;
import com.stylefeng.guns.modular.flagglory.service.IRobotService;
import javax.validation.Valid;
import java.util.List;

/**
 * 机器人控制器
 *
 * @author fengshuonan
 * @Date 2018-01-31 16:39:27
 */
@Controller
@RequestMapping("/robot")
public class RobotController extends BaseController {

    private String PREFIX = "/flagGlory/robot/";

    @Autowired
    private IRobotService robotService;
    @Autowired
    private ILevelService levelService;
    @Autowired
    private IDivisionService divisionService;

    /**
     * 跳转到机器人首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "robot.html";
    }

    /**
     * 跳转到添加机器人
     */
    @RequestMapping("/robot_add")
    public String robotAdd(Model model) {
        List<Level> levels = levelService.selectList(null);
        model.addAttribute("levels", levels);
        List<Division> divisions = divisionService.selectList(null);
        model.addAttribute("divisions", divisions);
        return PREFIX + "robot_add.html";
    }

    /**
     * 跳转到修改机器人
     */
    @RequestMapping("/robot_update/{robotId}")
    public String robotUpdate(@PathVariable Integer robotId, Model model) {
        Robot robot = robotService.selectById(robotId);
        model.addAttribute("item", robot);
        List<Level> levels = levelService.selectList(null);
        model.addAttribute("levels", levels);
        List<Division> divisions = divisionService.selectList(null);
        model.addAttribute("divisions", divisions);
        LogObjectHolder.me().set(robot);
        return PREFIX + "robot_edit.html";
    }

    /**
     * 获取机器人列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return robotService.selectList(null);
    }

    /**
     * 新增机器人
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Valid Robot robot, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        robotService.insert(robot);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除机器人
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer robotId) {
        robotService.deleteById(robotId);
        return SUCCESS_TIP;
    }

    /**
     * 修改机器人
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid Robot robot, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        robotService.updateById(robot);
        return super.SUCCESS_TIP;
    }

    /**
     * 机器人详情
     */
    @RequestMapping(value = "/detail/{robotId}")
    @ResponseBody
    public Object detail(@PathVariable("robotId") Integer robotId) {
        return robotService.selectById(robotId);
    }
}
